//index.js
//获取应用实例
const app = getApp()
import {
    $
} from '../../utils/util.js'
const mapKey = '2QEBZ-77K6J-CFDFO-FWGBD-UMOWS-VYBZX'
Page({
    data: {
        windowInfo: {},
        toView: 'hot',
        currentIndex: 0,
        scrollTop: 0,
        selectFood: {},
        content: [
            {
                type: 'hot',
                name: "热门",
                child: [{
                    name: "肉松热狗",
                    image: '../../images/1.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "这是美味的肉松热狗",
                    number: 1000,
                    sold: 200,
                    id: 1
                }, {
                    name: "皮豌豆",
                    image: '../../images/2.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "香脆可口的酸辣豌豆",
                    number: 1000,
                    sold: 200,
                    id: 2
                }, {
                    name: "蛊中有肉",
                    image: '../../images/3.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "美味的煲肉汤",
                    number: 1000,
                    sold: 200,
                    id: 3
                }, {
                    name: "可可豆腐",
                    image: '../../images/4.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "神奇的豆腐甜品",
                    number: 1000,
                    sold: 200,
                    id: 4
                }]
            },
            {
                type: 'free',
                name: "优惠",
                child: [{
                    name: "肉松热狗",
                    image: '../../images/1.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "这是美味的肉松热狗",
                    number: 1000,
                    sold: 200,
                    id: 5
                }, {
                    name: "皮豌豆",
                    image: '../../images/2.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "香脆可口的酸辣豌豆",
                    number: 1000,
                    sold: 200,
                    id: 6
                }, {
                    name: "蛊中有肉",
                    image: '../../images/3.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "美味的煲肉汤",
                    number: 1000,
                    sold: 200,
                    id: 7
                }, {
                    name: "可可豆腐",
                    image: '../../images/4.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "神奇的豆腐甜品",
                    number: 1000,
                    sold: 200,
                    id: 8
                }]
            },
            {
                type: 'dessert',
                name: "甜点",
                child: [{
                    name: "肉松热狗",
                    image: '../../images/1.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "这是美味的肉松热狗",
                    number: 1000,
                    sold: 200,
                    id: 9
                }, {
                    name: "皮豌豆",
                    image: '../../images/2.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "香脆可口的酸辣豌豆",
                    number: 1000,
                    sold: 200,
                    id: 10
                }, {
                    name: "蛊中有肉",
                    image: '../../images/3.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "美味的煲肉汤",
                    number: 1000,
                    sold: 200,
                    id: 11
                }, {
                    name: "可可豆腐",
                    image: '../../images/4.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "神奇的豆腐甜品",
                    number: 1000,
                    sold: 200,
                    id: 12
                }]
            },
            {
                type: 'drinks',
                name: "酒水",
                child: [{
                    name: "肉松热狗",
                    image: '../../images/1.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "这是美味的肉松热狗",
                    number: 1000,
                    sold: 200,
                    id: 13
                }, {
                    name: "皮豌豆",
                    image: '../../images/2.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "香脆可口的酸辣豌豆",
                    number: 1000,
                    sold: 200,
                    id: 14
                }, {
                    name: "蛊中有肉",
                    image: '../../images/3.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "美味的煲肉汤",
                    number: 1000,
                    sold: 200,
                    id: 15
                }, {
                    name: "可可豆腐",
                    image: '../../images/4.jpg',
                    price: 1000,
                    discount: 1500,
                    intr: "神奇的豆腐甜品",
                    number: 1000,
                    sold: 200,
                    id: 16
                }]
            }
        ],
        contentHeight: [],
        shopCars: [],
        show: false
    },

    goaway(){
        let plugin = requirePlugin("subway");
        let key = mapKey;//使用在腾讯位置服务申请的key;
        let referer = ''; //调用插件的app的名称
        wx.navigateTo({
            url: 'plugin://subway/index?key=' + key + '&referer=' + referer
        });
    },
    setToView(e) {
        let type = e.target.dataset['type']
        let index = e.target.dataset['index']
        this.setData({
            toView: type,
            currentIndex: index
        })
    },
    scroll(e) {
        let scrollTop = e.detail.scrollTop;
        let contentHeight = this.data.contentHeight;
        let content = this.data.content;
        let currentIndex = 0
        for (let i = 0; i < contentHeight.length - 1; i++) {
            let height1 = contentHeight[i];
            let height2 = contentHeight[i + 1];
            if (height1 <= scrollTop && scrollTop < height2) {
                currentIndex = i
                this.setData({
                    currentIndex: currentIndex
                })
            }
        }
    },
    async calculateHeigth() {
        let domHeight = [];
        domHeight.push(0)
        for (let i = 0; i < this.data.content.length; i++) {
            let item = this.data.content[i]
            let rect = await $(item.type)
            let length = domHeight.length;
            if (length) {
                domHeight.push(rect.height + domHeight[length - 1])
            } else {
                domHeight.push(rect.height)
            }
        }
        this.setData({
            contentHeight: domHeight
        })
    },
    handleFoodPar(e) {
        let food = e.currentTarget.dataset['food']
        this.setData({
            selectFood: food,
            show: true
        })
    },
    handleShopCar(e) {
        let child = e.currentTarget.dataset['child'];
        let type = e.currentTarget.dataset['type'];
        let content = this.data.content;
        for (let i = 0; i < content.length; i++) {
            let fChild = content[i].child;
            for (let j = 0; j < fChild.length; j++) {
                if (fChild[j].id == child.id) {
                    if (type) {
                        fChild[j].count ? (fChild[j].count++) : (fChild[j].count = 1);
                    } else {
                        fChild[j].count > 0 ? (fChild[j].count--) : (fChild[j].count = null);
                    }
                    child = fChild[j]
                }
            }
        }

        let shopCars = content.filter((item) => {
            if (item.count) {
                return item
            }
        })

        this.setData({
            content: content,
            selectFood: child,
            shopCars: shopCars
        })
    },
    handleClose() {
        this.setData({
            selectFood: {},
            show: false
        })
    },
    onLoad() {
        wx.getSystemInfo({
            success: res => {
                app.globalData.systemInfo = res;
                this.setData({
                    windowInfo: app.globalData.systemInfo
                })
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
        this.calculateHeigth()
        try {

        }catch (e) {
            console.log(e)
        }
    }
})
